package cn.gson.crm.controller;

import cn.gson.crm.model.mapper.CenterMapper;
import cn.gson.crm.model.pojos.*;
import cn.gson.crm.model.service.CustomerService;
import cn.gson.crm.model.service.MergeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @Autowired
    MergeService mergeService;

    @Autowired
    CenterMapper centerMapper;

    public Integer fouyuan(HttpSession session){
        User login = (User)session.getAttribute("login");
        int ygId=login.getyId();
        //查询是否为老板
        List<User> userList=centerMapper.selectUserRole(ygId);
        List<Role> roleList=userList.get(0).getRoleList();
        for(int i=0;i<roleList.size();i++){
            if(roleList.get(i).getjZil()==1){
                return 0;
            }
        }
        int ygId2= (int) login.getEmployees().get(0).getYgId();
        return ygId2;
    }

    //customer查询所有
    @RequestMapping("/all")
    @ResponseBody
    public Map<String,Object> all(Integer pageNum,Integer pageSize,String source,String name,String type){
        if(source.equals("全部客户")){
            source="";
        }
        if(type.equals("全部类型")){
            type="";
        }
        Page<Object> page = PageHelper.startPage(pageNum,pageSize);
        customerService.all(source,name,type);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        return map;
    }

    //中国地图
    @RequestMapping("/mapCustomer")
    @ResponseBody
    public List<Customer> mapCustomer(){
        return customerService.mapCustomer();
    }

    //customer删除：把客户修改为失效客户
    @RequestMapping("/delete")
    @ResponseBody
    public int delete(int customerId){
        return customerService.delete("失效客户",customerId);
    }


    //customer查询出所有的负责人
    @RequestMapping("/selectEmp")
    @ResponseBody
    public List<Employee> selectEmp(){
        return customerService.selectEmp(0);
    }

    //customer查询出所有的负责人
    @RequestMapping("/selectEmpCustomer")
    @ResponseBody
    public List<Employee> selectEmpCustomer(){
        return customerService.selectEmpCustomer(0);
    }

    //cusUpdate:客户信息查询所有:yg_id不为空
    @RequestMapping("/selectUpdate")
    @ResponseBody
    public Map<String,Object> selectUpdate(Integer pageNum, Integer pageSize, String dele, String name, String type, HttpSession session){
        int ygId;
        ygId = fouyuan(session);
        if(dele.equals("全部客户")){
            dele="";
        }
        if(type.equals("全部类型")){
            type="";
        }
        List<Customer> customers = customerService.SelectUpdate(ygId,dele, name, type);
        Page<Object> page = PageHelper.startPage(pageNum,pageSize);
        customerService.SelectUpdate(ygId,dele, name,type);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        map.put("customer",customers);
        return map;
    }

    //cusUpdate:客户新增或修改
    @RequestMapping("/add")
    @ResponseBody
    public void add(@RequestBody Customer customer){
        customer.setCustomerDele("潜在客户");
        System.err.println("controller:"+customer.getEmployee().toString());
        customerService.addUpdate(customer);
    }

    @RequestMapping("/importCustomer")
    @ResponseBody
    public Map<String, Object> addCustomer(String customer) throws ParseException {
        return customerService.importCustomer(customer);
    }

    //cusUpdate:客户新增或修改
    @RequestMapping("/addUpdate")
    @ResponseBody
    public void addUpdate(@RequestBody Customer customer){
        customer.setCustomerDele("潜在客户");
        customerService.addUpdate(customer);
       /* String ygId,String date,*/
       /* System.out.println("id:"+customer.getCustomerId());
        System.out.println("leader:"+ygId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date util = sdf.parse(date);
        Date newDate = new Date(util.getTime());
        customer.setCustomerDate(newDate);
//        customer.setCustomerId(Long.parseLong(id));
//        System.out.println("id1:"+id);
        return customerService.addUpdate(Integer.parseInt(ygId),customer);*/
    }

    //detailed:根据客户id查询联系人
    @RequestMapping("/selectLinkman")
    @ResponseBody
    public List<Linkman> selectLinkman(int customerId){
        return customerService.selectLinkman(0,customerId,null);
    }

    //detailed:根据客户id查询负责人
    @RequestMapping("/selectYname")
    @ResponseBody
    public List<Customer> selectYname(int customerId){
        return customerService.selectYname(customerId);
    }

    //linkman:根据客户/负责人/联系人查询联系人
    @RequestMapping("/selectLink")
    @ResponseBody
    public Map<String,Object> selectLink(Integer pageNum,Integer pageSize,String linkName,HttpSession session){
        int ygId;
        ygId = fouyuan(session);
        List<Linkman> linkman = customerService.selectLinkman(ygId,0, linkName);
        Page<Object> page = PageHelper.startPage(pageNum,pageSize);
        customerService.selectLinkman(ygId,0,linkName);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        map.put("link",linkman);
        return map;
    }

    //删除客户
    @RequestMapping("/deleteCustomer")
    @ResponseBody
    public String deleteCustomer(int customerId){
        return  customerService.deleteCustomer(customerId);
    }

    //linkman根据ygId查询出所有的负责人
    @RequestMapping("/selectLinkEmp")
    @ResponseBody
    public List<Employee> selectLinkEmp(int ygId){
        return customerService.selectEmpCustomer(ygId);
    }

    //linkman:新增修改
    @RequestMapping("/addLink")
    @ResponseBody
    public void addLink(@RequestBody Linkman linkman){
        customerService.addLink(linkman);
    }

    //linkman:删除联系人
    @RequestMapping("/deleteLink")
    @ResponseBody
    public String deleteLink(int linkId){
        return customerService.deleteLink(linkId);
    }

    //客户合并权限
    @RequestMapping("/megerId")
    @ResponseBody
    public Map<String,Object> mergeId(HttpSession session){
        User login = (User)session.getAttribute("login");
        Map<String,Object> map = new HashMap<>();
        map.put("ygId",login.getEmployees().get(0).getYgId());
        map.put("yId",fouyuan(session));
        return map;
    }

    //meger:根据负责人id或者客户名称或者客户名称字母
    @RequestMapping("/selectMeger")
    @ResponseBody
    public List<Customer> selectMeger(String ygId,String customerName){
        if(ygId=="" || ygId==null){
            ygId="0";
        }
        System.out.println("ygId:"+ygId);
        System.out.println("customerName:"+customerName);
        return customerService.selectMeger(Integer.parseInt(ygId),customerName);
    }

    //时间线：销售机会
    @RequestMapping("/timesSales")
    @ResponseBody
    public List<Sales> timesSales(int customerId){
        return customerService.timesSales(customerId);
    }

    //时间线：销售机会
    @RequestMapping("/timesFollow")
    @ResponseBody
    public List<Follow> timesFollow(int customerId,int followId){
        return customerService.timesFollow(customerId,followId);
    }

    //时间线：销售机会
    @RequestMapping("/timesOrder")
    @ResponseBody
    public List<Order> timesOrder(int customerId,int orderId){
        return customerService.timesOrder(customerId,orderId);
    }

    //时间线：合同
    @RequestMapping("/timesContract")
    @ResponseBody
    public List<Contract> timesContract(int customerId,int contractId){
        return customerService.timesContract(customerId,contractId);
    }

    //时间线：回款计划
    @RequestMapping("/timesMoney")
    @ResponseBody
    public List<MoneyBackPlan> timesMoney(int customerId,int backId){
        return customerService.timesMoney(customerId,backId);
    }

    //时间线：回款记录
    @RequestMapping("/timesRefund")
    @ResponseBody
    public List<RefundRecord> timesRefund(int customerId,int refundId){
        return customerService.timesRefund(customerId,refundId);
    }

    //时间线：退货
    @RequestMapping("/timesReturn")
    @ResponseBody
    public List<ReturnGoods> timesReturn(int customerId,int returnId){
        return customerService.timesReturn(customerId,returnId);
    }

    //时间线：换货
    @RequestMapping("/timesExchange")
    @ResponseBody
    public List<ExchangeGoods> timesExchange(int customerId,int excId){
        return customerService.timesExchange(customerId,excId);
    }

    //时间线：交付计划
    @RequestMapping("/timesPlan")
    @ResponseBody
    public List<DeliveryPlan> timesPlan(int customerId,int planId){
        return customerService.timesPlan(customerId,planId);
    }

    //时间线：竞争对手
    @RequestMapping("/timesCompetitor")
    @ResponseBody
    public List<Competitor> timesCompetitor(int customerId,int comId){
        return customerService.timesCompetitor(customerId,comId);
    }

    //时间线：报价
    @RequestMapping("/timesOffer")
    @ResponseBody
    public List<Offer> timesOffer(int customerId,int offerId){
        return customerService.timesOffer(customerId,offerId);
    }

    //时间线：解决方案
    @RequestMapping("/timesSolution")
    @ResponseBody
    public List<Solution> timesSolution(int customerId,int solutionId){
        return customerService.timesSolution(customerId,solutionId);
    }

    //时间线：维修工单
    @RequestMapping("/timesRepair")
    @ResponseBody
    public List<RepairOrder> timesRepair(int customerId,int repairId){
        return customerService.timesRepair(customerId,repairId);
    };

    //时间线：客户投诉
    @RequestMapping("/timesComplaints")
    @ResponseBody
    public List<Complaints> timesComplaints(int customerId,int comId){
        return customerService.timesComplaints(customerId,comId);
    };

    //时间线：客户服务
    @RequestMapping("/timesService")
    @ResponseBody
    public List<cn.gson.crm.model.pojos.CustomerService> timesService(int customerId,int cusId){
        return customerService.timesService(customerId,cusId);
    };

    //时间线：所有
    @RequestMapping("/timeAll")
    @ResponseBody
    public List<Times> timeAll(int customerId){
        return customerService.timeAll(customerId);
    }

    //时间线：回款
    @RequestMapping("/timesMoneyAll")
    @ResponseBody
    public List<Times> timeMoneyAll(int customerId){
        return customerService.timesMoneyAll(customerId);
    }

    //时间线：退换货
    @RequestMapping("/timesGoods")
    @ResponseBody
    public List<Times> timesGoods(int customerId){
        return customerService.timesGoods(customerId);
    }

    //客户合并
    @RequestMapping("/updateMerge")
    @ResponseBody
    public List<String> updateMerge(int ygId, int customerId, int byCustomerId){
        return mergeService.updateMerge(ygId,customerId,byCustomerId);
    }

    //客户合并数据
    @RequestMapping("/mergeSelect")
    @ResponseBody
    public List<Merge> mergeSelect(){
        return mergeService.mergeSelect();
    }

    //时间线：销售机会查询
    @RequestMapping("/timeSales")
    @ResponseBody
    public List<Sales> timeSales(int salesId){
        return mergeService.timeSales(salesId);
    }

    //统计客户总数
    @RequestMapping("/selectcustomer")
    @ResponseBody
    public List<Shuliang> selectcustomer(){
        return customerService.selectcustomer();
    }
}
